// narrager.js
var util = require('../../utils/util.js')
const app = getApp()
let that
let userInfo
let numberCount
Page({
  data: {
    inputValue: '',
    showTopTips: false,
    radioItems: [
      { name: '红色火箭', value: 'rocketRed', checked: true },
      { name: '橙色火箭', value: 'rocketOrange' },
      { name: '蓝色火箭', value: 'rocketBlue' },
      { name: '蓝黄火箭', value: 'rocketBlYe' },
      { name: '绿色火箭', value: 'rocketGreen' },
      { name: '红黄火箭', value: 'rocketReYe' }
    ],
    isLoading: 0,
    isdisabled: 0
  },

  onLoad: function (options) {
    that = this
    userInfo = wx.getStorageSync('userInfo') || false
    if (userInfo) app.reg(userInfo)
    wx.setNavigationBarTitle({
      title: "倩狐互动-弹幕",
    })
    numberCount = wx.getStorageSync("number") || 0
    console.log("numberCount: " + numberCount)
    if (numberCount === -1) {
      that.setData({
        showTopTips: true
      })
      setTimeout(function () {
        that.setData({
          showTopTips: false
        })
      }, 2000)
    }
  },

  onShareAppMessage: function () {

  },

  launch: function () {
    if (that.data.inputValue.length === 0) {
      wx.showLoading({
        title: '请填写弹幕',
        duration: 1000
      })
    } else {
      if (that.data.timer === -1) {
        wx.showLoading({
          title: '弹幕数用完了',
          duration: 2000
        })
      }
      else {
        wx.showLoading({
          title: '火箭发射中...'
        })
        that.setData({
          isLoading: 1,
          isdisabled: 1
        })
        setTimeout(() => that.submitBarrager(), 3000)
      }
    }
  },

  submitBarrager: function () {
    let style = "rocketOrange"
    var radioItems = that.data.radioItems
    for (var i = 0, len = radioItems.length; i < len; ++i) {
      if (radioItems[i].checked) {
        style = radioItems[i].value
      }
    }
    app.ajax({
      url: "/wechat/Barrager/add",
      data: { "openId": userInfo.openId, "style": style, "note": that.data.inputValue },
      success: function (json) {
        if (json.res === 1) {
          util.addLogs("发送弹幕成功！")
          wx.hideLoading()
          wx.showToast({
            title: '发送成功！',
            icon: 'succes',
            duration: 1000,
            mask: true
          })
          that.setData({
            inputValue: '',
            isLoading: 0,
            isdisabled: 0
          })
        }
        else {
          that.setData({
            showTopTips: true
          })
          wx.setStorageSync("number", -1)
          setTimeout(function () {
            that.setData({
              showTopTips: false,
              inputValue: '',
              isLoading: 0,
              isdisabled: 0
            })
          }, 2000)
        }
      }
    })
  },

  radioChange: function (e) {
    let radioItems = that.data.radioItems
    for (var i = 0, len = radioItems.length; i < len; ++i) {
      radioItems[i].checked = radioItems[i].value == e.detail.value
    }
    that.setData({
      radioItems: radioItems
    })
  },

  bindKeyInput: function (e) {
    that.setData({
      inputValue: e.detail.value
    })
  }
});